package com.sniff.atp.modules.test.process;

import com.ckbs.global.common.utils.DingTalkNoticeUtils;
import com.sniff.atp.modules.test.model.DemandReport;
import com.sniff.atp.modules.test.model.PublishPlan;
import com.sniff.atp.modules.test.service.DemandReportService;
import com.sniff.atp.modules.test.service.PublishPlanService;
import jodd.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @ProjectName: sniff-atp
 * @Package: com.sniff.atp.modules.test.process
 * @ClassName: BusinessPublishPlan
 * @Description: 通知业务方 发布通告
 * @Author: zss
 * @CreateDate: 2023/11/2 19:36
 * @UpdateDate: 2023/11/2 19:36
 * @Version: 1.0
 */

@Component
@Configuration
@EnableScheduling
public class BusinessPublishPlan {

    @Value("${sniff.autoInfoFlag}")
    private String autoInfoFlag;

    @Autowired
    private PublishPlanService publishPlanService;

    @Autowired
    private DemandReportService demandReportService;


//    @Scheduled(cron = "0/5 * * * * ?")
    @Scheduled(cron = "0 0 23 * * ?")
    private void configureTasks(){
        System.out.println("111111111111111");

        // 获取当前日期
        Calendar calendar = Calendar.getInstance();
//        calendar.add(Calendar.DAY_OF_MONTH, -4);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);

        // 获取当天的0点时间的Date对象
        Date midnightDate = calendar.getTime();

        // 获取当前日期
        LocalDate currentDate = LocalDate.now();

        // 定义日期格式
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");

        PublishPlan publishPlan = new PublishPlan();
        publishPlan.setInfoTime(midnightDate);
        List<PublishPlan> publishPlanList = publishPlanService.qryBusinessPublishPlan(publishPlan);

        if(!publishPlanList.isEmpty()){
            //通知内容
            String content = "";
            content = content.concat("------发布通告-----\n发布日期: ").concat(currentDate.format(formatter)).concat("\n");
            content = content.concat("发布时间: 22点 \n").concat("发布内容: \n \n");

            //获取需求id
            List<Long> demandIdList = publishPlanList.stream().map(PublishPlan::getDemandId).collect(Collectors.toList());
            //获取需求list
            List<DemandReport> demandReportList = demandReportService.qryDemandInfoByIdList(demandIdList);

            //发布计划的list中增加一个PD的字段
            publishPlanList.forEach(entity1 ->
                    demandReportList.stream()
                            .filter(entity2 -> entity2.getDemandId().equals(entity1.getDemandId()))
                            .findFirst()
                            .ifPresent(entity2 -> {
                                entity1.setDemandPd(entity2.getDemandPd());
                            })
            );

            String addContent = "";
            //有值，通知客户
            for(int i=0;i<publishPlanList.size();i++){
                addContent = addContent.concat("【").concat(publishPlanList.get(i).getPrefixDemandBug()).concat("】");
                addContent = addContent.concat(publishPlanList.get(i).getDemandPrdInfo());
                //增加PD
                String demandPD = publishPlanList.get(i).getDemandPd();
                if(StringUtil.isNotBlank(demandPD) && !"".equals(demandPD)){
//                    hasDevelopFlag = true;
                    addContent = addContent.concat(" （产品：");
                    addContent = addContent.concat(demandPD).concat(")").concat("\n");
                }else{
                    addContent = addContent.concat("\n");
                }
            }
            //两个通知内容合并
            content = content.concat(addContent);

            //发布通知业务群
            String robotToken ="6d284ba7dcb389578dbfac0a70a5687684237e940ebc16e6a4efa9afdc5b50a4";
            String robotKey = "SEC56237a3e23f605a513d400a7dc5d3767e2c01b2defc0ee684518dd0d669c0a28";

//            String robotToken = "c6c2c7c6c98c2fb5d234319afa9e0dad659152a8518ac739191811ce1bf8a67b";
//            String robotKey = "SECdf56e788259528d23a4b225863526a0bc426c178251313f1e4d5bdc5e9b696d6";

            List<String> mobileList = new ArrayList<>();
            mobileList.add("15541150211");


            if("true".equals(autoInfoFlag)){
                DingTalkNoticeUtils.sendDingMsgAll(robotKey,robotToken,mobileList,content);
            }

        }

    }
}
